﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class Email
    {
        #region Properties
        public Status Status
        {
            get
            {
                return this.GetStatus();
            }
        }
        #endregion

        #region Methods

        public Status GetStatus()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Status.Where(p => p.ID == this.ID_Status);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<ErrorItem> SendEmail(int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            //Send

            int errorMess = _db.SendEmail(id_User);

            
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Static Methods
        public static Email Parse(System.Object value)
        {
            Email temp = null;
            try
            {
                temp = (Email)value;
            }
            catch (Exception ex)
            {
            }
            return temp;
        }

        public static List<Email> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Emails;
            if (v.Count() == 0)
                return null;
            return v.OrderByDescending(p => p.CreatedTime).ToList();
        }

        public static List<Email> GetListAvailable()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Emails.Where(p => p.ID_Status != 2);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Email> GetListNotAvailable()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Emails.Where(p => p.ID_Status == 2);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Email email)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (email.ID_Status < 1)
                errorList.Add(new ErrorItem("ID_Status", "ID_Status is required."));
            if (email.Tenor == "")
                errorList.Add(new ErrorItem("Tenor", "Tenor is required."));
            if (email.Tenor.Length > 3000)
                errorList.Add(new ErrorItem("Tenor", "Tenor is only allow up to 3000 characters."));
            if (email.Title == "")
                errorList.Add(new ErrorItem("Title", "Title is required."));
            if (email.Title.Length > 300)
                errorList.Add(new ErrorItem("Title", "Title is only allow up to 300 characters."));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Email ii, int id_User, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ii);
            if (errorList.Count == 0)
            {
                Nullable<Int32> id_Email = new int();
                int errorMess = _db.InsertEmail(id_User, ref id_Email, ii.ID_Language, ii.Title, ii.Tenor);
                if (errorMess == 1)
                    ii.ID_Email = id_Email.Value;
                
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Email ic, int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

                Email oi = _db.Emails.Single(p => p.ID_Email == ic.ID_Email && p.ID_Language == ic.ID_Language);
                String content = "";
                content = oi.ID_Language + "_" + oi.Title + "_" + oi.Tenor + "_" + oi.ID_Status;
                int errorMess = _db.ChangeEmail(id_User, ic.ID_Email, ic.ID_Language, ic.Title, ic.Tenor, ic.ID_Status, content, 0, 0);
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Email ic, int id_User, int id_Log, int id_Lang)
        {
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

                Email oi = _db.Emails.Single(p => p.ID_Email == ic.ID_Email && p.ID_Language == ic.ID_Language);
                int errorMess = _db.ChangeEmail(id_User, ic.ID_Email, ic.ID_Language, ic.Title, ic.Tenor, ic.ID_Status, "", id_Log, 1);
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> Delete(int id_Email, int id_User, int deep, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = new List<ErrorItem>();

            int errorMess = _db.DeleteEmail(id_User, id_Email, deep);
            
            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Search
        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (query.Length != 0)
                    query += " AND ";
                if (kvp.Key == "ID_Email" || kvp.Key == "ID_Language" || kvp.Key == "ID_Status")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "Tenor" || kvp.Key == "Title")
                {
                    if (exactList[kvp.Key])
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " = '" + kvp.Value.ToString() + "'";
                    }
                    else
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " LIKE '%" + kvp.Value.ToString() + "%'";
                    }
                }
                else if (kvp.Key == "CreatedTime")
                {
                    if (DateTime.Parse(kvp.Value.ToString()) != noValue)
                        query += "CreatedTime" + " = " + DateTime.Parse(kvp.Value.ToString()).ToString("yyyy-MM-dd") + "";
                }
            }

            if (parametersList.ContainsKey("CreatedbeginDate") && parametersList.ContainsKey("CreatedendDate"))
            {
                query += " DATEDIFF(day, " + DateTime.Parse(parametersList["CreatedendDate"].ToString()).ToString("yyyy-MM-dd") + " , CreatedTime) >= 0";
                query += " DATEDIFF(day, CreatedTime , " + DateTime.Parse(parametersList["CreatedbeginDate"].ToString()).ToString("yyyy-MM-dd") + ") >= 0";
            }

            return query;
        }

        public static System.Object GetEmail(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);
            var v = _db.ExecuteQuery<Email>(query);
            if (oneRow)
            {
                if (v.Count() == 0)
                    return null;
                return v.First();
            }
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }
        #endregion
    }
}